import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/pages/home_provider.dart';
import 'package:flutter_app/pages/series_page.dart';
import 'package:flutter_app/pages/piazza_page.dart';
import 'package:flutter_app/pages/user_center_page.dart';
import 'package:flutter_app/res/colours.dart';
import 'package:flutter_app/routers/router_path.dart';
import 'package:flutter_app/routers/router_utils.dart';
import 'package:flutter_app/utils/log_util.dart';
import 'package:provider/provider.dart';
import 'package:sp_util/sp_util.dart';

import '../constant.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomeState();
}

class _HomeState extends State<HomePage> {
  static const double _imageWH = 25.0;

  List<Widget> _bodyPages;
  List<BottomNavigationBarItem> _bottomNavigationBarItems;
  List<String> _labels = ['商城', '体系', '我的'];
  PageController _pageController = PageController();
  HomeProvider _provider = HomeProvider();

  @override
  void initState() {
    super.initState();
    _bodyPages = [PiazzaPage(), SeriesPage(), UserCenterPage()];
    _buildNavigationBarItem();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void _buildNavigationBarItem() {
    if (_bottomNavigationBarItems == null) {
      const items = [
        [
          Icon(CupertinoIcons.home, color: Colours.ff8c929a, size: _imageWH),
          Icon(CupertinoIcons.home, color: Colours.ff0692ff, size: _imageWH)
        ],
        [
          Icon(CupertinoIcons.bitcoin_circle_fill,
              color: Colours.ff8c929a, size: _imageWH),
          Icon(CupertinoIcons.bitcoin_circle_fill,
              color: Colours.ff0692ff, size: _imageWH)
        ],
        [
          Icon(CupertinoIcons.profile_circled,
              color: Colours.ff8c929a, size: _imageWH),
          Icon(CupertinoIcons.profile_circled,
              color: Colours.ff0692ff, size: _imageWH)
        ],
      ];
      _bottomNavigationBarItems = List.generate(
          items.length,
          (index) => BottomNavigationBarItem(
              icon: items[index][0],
              activeIcon: items[index][1],
              label: _labels[index]));
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (_) => _provider,
        child: Scaffold(
          backgroundColor: Colors.white60,
          bottomNavigationBar: Consumer<HomeProvider>(
              builder: (BuildContext context, provider, Widget child) {
            return BottomNavigationBar(
              items: _bottomNavigationBarItems,
              backgroundColor: Colors.white,
              elevation: 5,
              iconSize: _imageWH,
              selectedFontSize: 12,
              unselectedFontSize: 12,
              currentIndex: provider.value,
              type: BottomNavigationBarType.fixed,
              onTap: (index) {
                var string = SpUtil.getString(Constant.USER_ID);
                LogUtil.v('============>$string');
                if (index == 2 && SpUtil.getString(Constant.USER_ID).isEmpty) {
                  _goLogin();
                } else {
                  _pageController.jumpToPage(index);
                }
              },
            );
          }),
          body: PageView(
            physics: const NeverScrollableScrollPhysics(), // 禁止滑动
            controller: _pageController,
            children: _bodyPages,
            onPageChanged: (index) {
              _provider.value = index;
            },
          ),
        ));
  }

  void _goLogin() {
    RouterUtils.push(context, RouterPath.login);
  }
}
